<?php

class Employment extends FaceintegrationBaseModel {

    public $image;

    public static function getElementById($nid) {
        $node = node_load($nid);
        if ($node !== FALSE)
            return Employment::toModel($node);
        return NULL;
    }

    public static function getElementsByCategory($tid = NULL, $from = 0, $limit = 4) {
        if ($tid == NULL) {
            $query = new EntityFieldQuery();
            $queryResult = $query->entityCondition('entity_type', 'node')
                    ->propertyCondition('type', 'employment')
                    ->propertyOrderBy('created', 'DESC')
                    ->range($from, $limit)
                    ->execute();
        }
        $result = array();
        if (isset($queryResult['node'])) {
            foreach ($queryResult['node'] as $key => $value) {
                $model = Employment::getElementById($key);
                if ($model != NULL)
                    $result[] = $model;
            }
        }
        return $result;
    }

    public static function toModel($node) {
        $model_employment = new Employment();
        FaceintegrationBaseModel::initialize($node, $model_employment);
        $image_employment = field_get_items('node', $node, 'field_employment_image');
        if (count($image_employment) > 0)
            $model_employment->image = file_create_url($image_employment[0]['uri']);
        return $model_employment;
    }

    public static function selectModel() {
        $query = db_select('node_type', 'nt')
        ->condition('nt.type', 'employment')
        ->fields('nt',array('description'))
        ->execute()->fetchField();
        return $query; 
    }

}

?>
